import { _decorator, Component, EventTouch, Label, log, Node } from 'cc';
import { Money } from './Money';
import { GameManager } from './GameManager';
import { AWARYTYPE, EPOCH, LV } from '../AshFramework/Datas/GameConstant';
import { EventManager } from '../AshFramework/Managers/EventManager';
import Banner from '../Banner';
import { GameUtil } from './GameUtil';
const { ccclass, property } = _decorator;

@ccclass('MenuManager')
export class MenuManager extends Component {
    private static _instance: MenuManager = null;

    public static get Instance(): MenuManager {
        if (!MenuManager._instance) {
            MenuManager._instance = new MenuManager();
        }
        return MenuManager._instance;
    }

    @property(Node)
    UpgradePanel: Node = null;

    @property(Node)
    BattlePanel: Node = null;

    @property(Node)
    ShopPanel: Node = null;

    @property(Node)
    CardPanel: Node = null;

    @property(Node)
    EpochNode: Node = null;

    @property(Node)
    BattleNode: Node = null;

    @property(Node)
    LvNode: Node = null;

    @property(Node)
    GoldNode: Node = null;

    @property(Node)
    MasonryNode: Node = null;

    private _targetNode: Node | null = null;
    private _targetPanel: Node | null = null;

    protected onLoad(): void {
        MenuManager._instance = this;
    }

    clickBtn(event: EventTouch, type: string) {
        if (this._targetNode) {
            this._targetNode.active = false;
        }
        let target: Node = event.getCurrentTarget();
        this._targetNode = target.getChildByName("选中");
        this._targetNode.active = true;

        let panel: Node = null;
        switch (type) {
            case "1":
                panel = this.UpgradePanel;
                break;
            case "2":
                panel = this.BattlePanel;
                break;
            case "3":
                panel = this.CardPanel
                break;
            case "4":
                panel = this.ShopPanel;
                break;
        }
        if (this._targetPanel && this._targetPanel != panel) {
            this._targetPanel.active = false;
        }
        this._targetPanel = panel
        this._targetPanel.active = true;
    }


    updateGold(num: number = GameManager.Instance.userData.gold) {
        this.GoldNode.getComponent(Money).setMoney(num);
    }

    addGold(num: number) {
        GameManager.Instance.userData.gold += num;
        this.GoldNode.getComponent(Money).addMoney(num);
    }

    updateMasonry(num: number = GameManager.Instance.userData.masonry) {
        this.MasonryNode.getComponent(Money).setMoney(num);
    }

    addMasonry(num: number) {
        GameManager.Instance.userData.masonry += num;
        this.MasonryNode.getComponent(Money).addMoney(num);
    }

    updateEpoch(epoch: EPOCH) {
        this.EpochNode.getComponent(Label).string = epoch;
    }

    updateLv(epoch: LV) {
        this.LvNode.getComponent(Label).string = epoch;
    }

    getMasonryWorldPos() {
        return this.MasonryNode.getWorldPosition().clone();
    }

    protected onEnable(): void {
        this.updateGold();
        this.updateMasonry();
        this.updateEpoch(GameManager.Instance.userData.epoch);
        this.updateLv(GameManager.Instance.userData.lv);
        EventManager.Scene.emit(Banner.CB_BannerOnSize);

        //将登陆界面设置为战斗界面

        if (!this._targetPanel) {
            this._targetPanel = this.BattlePanel;
            this._targetPanel.active = true;
            this._targetNode = this.BattleNode;
            this._targetNode.active = true;
        }
    }
}


